//
//  WssEngine.swift
//  MobileRTCSwiftSample
//
//  Created by 张衡 on 2019/11/7.
//  Copyright © 2019 Zoom Video Communications, Inc. All rights reserved.
//

import Foundation


/////////////////     通用权利       /////////////////

let mt_wss_raiseHand             = 1     //举手
let mt_wss_downHand              = 2     //放下举手

let mt_wss_qcloud                = 8     //发送字幕
let mt_wss_barrage               = 9     //发送弹幕
let mt_wss_enter_audience        = 10    //直播观众进场（仅适用直播模式）
let mt_wss_exit_audience         = 11    //直播观众离场（仅适用直播模式）

let mt_wss_enter_toAudience      = 12    //直播观众进场发指定观众（仅适用直播模式）

let mt_wss_reqUpmic              = 13    //请求上麦（仅适用直播模式）
let mt_wss_reqDownmic            = 14    //请求下麦（仅适用直播模式）
let mt_wss_send2UpMic            = 15    //二次请求上麦
let mt_wss_acceptUpmic           = 16    //二次请求通过回复


////////////////       主持人      //////////////////

let mt_wss_host_muteAll          = 104  //全体静音
let mt_wss_host_unmuteAll        = 105  //全体静音解除
let mt_wss_host_stopShare        = 106  //禁止共享
let mt_wss_host_allowShare       = 107  //允许共享
let mt_wss_host_downAllHand      = 108  //放下所有举手
let mt_wss_host_downUserHand     = 3    //放下某个用户的举手
let mt_wss_host_removeUser       = 110  //移出某个用户
let mt_wss_host_muteUser         = 111  //静音某个用户A
let mt_wss_host_unMuteUser       = 112  //取消某个用户的静音
let mt_wss_host_focuseUser       = 113  //将某个用户设为焦点
let mt_wss_host_meetingTimeout   = 114  //会议超时
let mt_wss_host_exit             = 102  //结束会议

let mt_wss_host_closeVideoAll    = 120  //关闭全部视频
let mt_wss_host_showVideoAll     = 121  //开启全部视频
let mt_wss_host_closeVideoUser   = 115  //关闭用户视频
let mt_wss_host_showVideoUser    = 116  //开启用户视频


let mt_wss_host_swAudience       = 125    //设置观众角色（仅适用直播模式）
let mt_wss_host_swAnchor         = 126    //设置主播角色（仅适用直播模式）
let mt_wss_host_disDanmu         = 127    //禁止聊天
let mt_wss_host_danmu            = 128    //开启聊天
let mt_wss_host_notAllowToRole   = 129    //上麦关闭
let mt_wss_host_allowToRole      = 130    //上麦开启

let mt_wss_host_sendQuestionnaire      = 131    //发送问卷（投票）
let mt_wss_host_sendTheAnswer          = 132    //发送答案（投票）
let mt_wss_host_saveNewQuestionnaire   = 133    //保存新问卷
let mt_wss_host_newMemGetQuestionnaire = 134    //新参会获取问卷（投票）

let mt_wss_modifyName                  = 137    //修改名字
let mt_wss_message                     = 140    //指向性通知


class WssEngine:NSObject  {
    
    static let shared = WssEngine()
    
    func connect() -> () {
        self.socket.delegate = WssDeleEngine.shared
        self.socket.connect()
    }
    
    func disconnect() -> () {
        self.socket.disconnect()
    }
    
    lazy var socket: WebSocket = {
        let sock = WebSocket(url: URL(string: "wss://talk.1meeting.com:9501")!)
        return sock
    }()
    
    ////////////////////////////////////////////////
    
    //举手
    func handUp(userId:String=Td.sh().params.userId!,isok:Bool) -> () {
        let model = WebSocketMode()
        model.roomid = Td.sh().params.roomId
        model.userid = userId
        let cmdCode = isok ? mt_wss_raiseHand:mt_wss_downHand
        model.cmd = cmdCode
         
        let mesg = model.toJSON()?.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        self.socket.write(data: data!)
    }
    
    //弹幕
    func barrage(userId:String=Td.sh().params.userId!,txt:String) {
        let model = WebSocketMode()
        model.roomid = Td.sh().params.roomId
        model.userid = userId
        model.cmd = mt_wss_barrage
        model.txt = txt
        let mesg = model.toJSON()?.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        self.socket.write(data: data!)
    }
     
    //字幕
    func realtime(userId:String=Td.sh().params.userId!,txt:String) {
        let model = WebSocketMode()
        model.roomid = Td.sh().params.roomId!
        model.userid = userId
        model.cmd = mt_wss_qcloud
        model.txt = txt
        let mesg = model.toJSON()?.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        self.socket.write(data: data!)
    }
    
    //观众
    func audience(userId:String=Td.sh().params.userId!,isEnter:Bool) -> () {
        let model = WebSocketMode()
        model.roomid = Td.sh().params.roomId
        model.userid = userId
        let cmd = isEnter ? mt_wss_enter_audience:mt_wss_exit_audience
        model.cmd = cmd
        let mesg = model.toJSON()?.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        self.socket.write(data: data!)
    }
     
    //观众指定观众
    func toAudience(userId:String=Td.sh().params.userId!,toUserId:String) {
         let model = WebSocketMode()
         model.roomid = Td.sh().params.roomId
         model.userid = userId
         model.touserid = toUserId
         model.cmd = mt_wss_enter_toAudience
         let mesg = model.toJSON()?.toJsonString()
         let data = mesg?.data(using: String.Encoding.utf8)
         //用命令退出全部设备
         self.socket.write(data: data!)
     }
     
    //请求上麦
    func upmic(userId:String=Td.sh().params.userId!,isok:Bool) -> () {
        let model = WebSocketMode()
        model.roomid = Td.sh().params.roomId
        model.userid = userId
        let cmd = isok ? mt_wss_reqUpmic:mt_wss_reqDownmic
        model.cmd = cmd
        let mesg = model.toJSON()?.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        self.socket.write(data: data!)
    }
     
    
    /////////////  主持人权利  ///////////
     
    //主持人弹幕
    func disDanmu(isok:Bool) -> () {
        let model = WebSocketMode()
        model.roomid = Td.sh().params.roomId
         let cmd = isok ? mt_wss_host_disDanmu:mt_wss_host_danmu
        model.cmd = cmd
        let mesg = model.toJSON()?.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        self.socket.write(data: data!)
    }
    
    //主持人
    func setRole(userId:String,isAnchor:Bool) -> () {
        let model = WebSocketMode()
        model.roomid = Td.sh().params.roomId
        model.userid = userId
        let cmd = isAnchor ? mt_wss_host_swAnchor:mt_wss_host_swAudience
        model.cmd = cmd
        let mesg = model.toJSON()?.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        self.socket.write(data: data!)
    }
       
    //是否全体静音
    func allMute(isok:Bool) -> () {
         let model = WebSocketMode()
         model.roomid = Td.sh().params.roomId
         let cmdCode = isok ? mt_cmd_host_muteAll:mt_cmd_host_unmuteAll
         model.cmd = cmdCode
         let mesg = model.toJSON()?.toJsonString()
         let data = mesg?.data(using: String.Encoding.utf8)
         //用命令退出全部设备
         self.socket.write(data: data!)
    }
     
    //是否允许上麦
    func notAllowToRole(isok:Bool) -> () {
        let model = WebSocketMode()
        model.roomid = Td.sh().params.roomId
         let cmd = isok ? mt_wss_host_notAllowToRole:mt_wss_host_allowToRole
        model.cmd = cmd
        let mesg = model.toJSON()?.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        self.socket.write(data: data!)
    }
     
    //踢人
    func kick(userId:String) -> () {
         let model = WebSocketMode()
         model.roomid = Td.sh().params.roomId
         model.cmd = mt_wss_host_removeUser
         model.userid = userId
         let mesg = model.toJSON()?.toJsonString()
         let data = mesg?.data(using: String.Encoding.utf8)
         //用命令退出全部设备
         self.socket.write(data: data!)
    }
    
    //关闭房间
    func exit(){
        let model = WebSocketMode()
        model.roomid = Td.sh().params.roomId
        model.cmd = mt_wss_host_exit
        let mesg = model.toJSON()?.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        self.socket.write(data: data!)
    }

    //主持人解除举手
    func downAllHand() -> () {
        let model = WebSocketMode()
        model.roomid = Td.sh().params.roomId
        model.cmd = mt_wss_host_downAllHand
        let mesg = model.toJSON()?.toJsonString()
        let data = mesg?.data(using: String.Encoding.utf8)
        //用命令退出全部设备
        self.socket.write(data: data!)
    }
    
    //主持人解除某用户举手
    func downUserHand(userId:String) -> () {
         let model = WebSocketMode()
        model.roomid = Td.sh().params.roomId
         model.cmd = mt_wss_host_downUserHand
         model.userid = userId
         let mesg = model.toJSON()?.toJsonString()
         let data = mesg?.data(using: String.Encoding.utf8)
         //用命令退出全部设备
         self.socket.write(data: data!)
    }
    
    //设定焦点
    func focusUser(userId:String) -> () {
         let model = WebSocketMode()
         model.roomid = Td.sh().params.roomId
         model.cmd = mt_wss_host_focuseUser
         model.userid = userId
         let mesg = model.toJSON()?.toJsonString()
         let data = mesg?.data(using: String.Encoding.utf8)
         //用命令退出全部设备
         self.socket.write(data: data!)
    }
    
    
    //是否用户开启关闭视频
    func videoUser(userId:String,isok:Bool) -> () {
         let model = WebSocketMode()
         model.roomid = Td.sh().params.roomId
         let cmd = isok ? mt_wss_host_showVideoUser:mt_wss_host_closeVideoUser
         model.cmd = cmd
         model.userid = userId
         let mesg = model.toJSON()?.toJsonString()
         let data = mesg?.data(using: String.Encoding.utf8)
         //用命令退出全部设备
         self.socket.write(data: data!)
    }
    
    //是否用户静音
    func muteUser(userId:String,isok:Bool) -> () {
         let model = WebSocketMode()
         model.roomid = Td.sh().params.roomId
         let cmd = isok ? mt_wss_host_muteUser:mt_wss_host_unMuteUser
         model.cmd = cmd
         model.userid = userId
         let mesg = model.toJSON()?.toJsonString()
         let data = mesg?.data(using: String.Encoding.utf8)
         //用命令退出全部设备
         self.socket.write(data: data!)
    }
    
    
    
}
